Generated code - The predicate system, SelfServicing

Preface

LLBLGen Pro's filtering capabilities are build using predicate objects, instantiated from predicate classes. This section describes these classes in depth, how to use them, what their purpose is and shows an example for every predicate. These classes are part of the low-level API of the LLBLGen Pro runtime framework. When using Linq, you'll never run into them directly, the Linq to LLBLGen Pro provider will convert your Linq query to these objects. QuerySpec however uses the predicate classes to construct filters. When possible, use the operator overloads to easily formulate predicates.

To quickly understand which predicate class you need, a handy table is provided below, which should help you converting a WHERE construct in SQL to LLBLGen Pro predicates.

Predicate classes to use for Database or in-memory filtering:
SQL Predicate class to use
Field BETWEEN 3 AND 5
Field BETWEEN field2 AND 4
FieldBetweenPredicate
Field = Field2
Field < (Field2 * 4)
FieldCompareExpressionPredicate
Field Is NULL FieldCompareNullPredicate
Field IN (1, 2, 3, 5) FieldCompareRangePredicate
Field IN (
SELECT Field FROM Foo WHERE ...)
FieldCompareSetPredicate
Field = 3
Field != "Foo"
FieldCompareValuePredicate
Field LIKE "Foo%" FieldLikePredicate
CONTAINS(Field, 'string'..) FieldFullTextSearchPredicate

Predicate classes to use for in-memory filtering only:

The predicate classes

LLBLGen Pro offers you a wide range of different predicate classes to define your filters with. Each predicate class which name starts with Field works on a field, so they all have the form field some expression/operator values. Sometimes this is not enough, you for example want to filter using the predicate (Field * 3) > OtherField. This can be accomplished by adding an expression to the field filtered on. See Field expressions and aggregates for more information about expressions.

All predicate classes implement the IPredicate interface and derive from the Predicate class located, as the predicate classes, in the ORMSupportClasses assembly. If you want to add a specific predicate to the pack already offered, you can: implement a class also deriving from Predicate and you're done.

To see the full class signatures and their methods, please consult the LLBLGen Pro reference manual's SD.LLBLGen.Pro.ORMSupportClasses namespace.
Constructing Predicate Expressions
In the paragraph Predicates and Predicate expressions, you were introduced to the concepts of predicates and predicate expressions. The following examples will show equivalents of the earlier examples in that paragraph to illustrate how to use native language contstructs to create predicate expressions. These predicate expressions are created by the overloaded operators & and | in C# and And and Or in VB.NET. To produce the same full filter as illustrated in Predicates and Predicate expressions, use the following code. It uses a single step, which skips the separate creation of filter A.

// C#
IPredicateExpression B = ((Table1Fields.Foo == "One") & (Table1Fields.Bar == "Two")) // A
	| (Table2Fields.Bar2 == "Three");
' VB.NET
Dim B As IPredicateExpression = ((Table1Fields.Foo = "One") And (Table1Fields.Bar = "Two")) _  ' A
	Or (Table2Fields.Bar2 = "Three")

It's also possible to negate a predicate with the native language operator ! (C#) or Not (VB.NET). Same example as previously, but now the last predicate is negated:

// C#
IPredicateExpression B = ((Table1Fields.Foo == "One") & (Table1Fields.Bar == "Two")) // A
	| !(Table2Fields.Bar2 == "Three");
' VB.NET
Dim B IPredicateExpression = ((Table1Fields.Foo = "One") And (Table1Fields.Bar = "Two")) _  ' A
	Or Not (Table2Fields.Bar2 = "Three")

To chain several predicates together into a single predicate expression, you can also consider the AddWithAnd and AddWithOr methods of the PredicateExpression class. Every (predicate Operator predicate) statement results in a PredicateExpression object. If you want to do this: (the example shows values in the WHERE clause, LLBLGen Pro always generates parameters for values, it never includes any value into the query directly

...
WHERE	TableFields.Foo = "One" OR 
		TableFields.Foo = "Two" OR 
		TableFields.Foo = "Three" OR
		TableFIelds.Foo = "Four"

you should use this code:

// C#
IPredicateExpression filter = 
	((TableFields.Foo=="One") | (TableFields.Foo=="Two"))
	.AddWithOr(TableFields.Foo=="Three")
	.AddWithOr(TableFields.Foo=="Four);
	
// which is equal to:
IPredicateExpression filter = new PredicateExpression();
filter.Add(TableFields.Foo=="One")
	.AddWithOr(TableFields.Foo=="Two")
	.AddWithOr(TableFields.Foo=="Three")
	.AddWithOr(TableFields.Foo=="Four);
	
// which is equal to:
IPredicateExpression filter = new PredicateExpression();
filter.Add(new FieldCompareValuePredicate(TableFields.Foo, ComparisonOperator.Equal, "One"))
	.AddWithOr(new FieldCompareValuePredicate(TableFields.Foo, ComparisonOperator.Equal, "Two"))
	.AddWithOr(new FieldCompareValuePredicate(TableFields.Foo, ComparisonOperator.Equal, "Three"))
	.AddWithOr(new FieldCompareValuePredicate(TableFields.Foo, ComparisonOperator.Equal, "Four"));
' VB.NET
Dim filter As IPredicateExpression = _
	((TableFields.Foo="One") Or (TableFields.Foo="Two")).AddWithOr(TableFields.Foo="Three").AddWithOr(TableFields.Foo="Four)
	
' which is equal to:
Dim filter As New PredicateExpression()
filter.Add(TableFields.Foo="One").AddWithOr(TableFields.Foo="Two").AddWithOr(TableFields.Foo="Three").AddWithOr(TableFields.Foo="Four)
	
' which is equal to:
Dim filter As New PredicateExpression()
filter.Add(New FieldCompareValuePredicate(TableFields.Foo, ComparisonOperator.Equal, "One")) _
	.AddWithOr(New FieldCompareValuePredicate(TableFields.Foo, ComparisonOperator.Equal, "Two")) _
	.AddWithOr(New FieldCompareValuePredicate(TableFields.Foo, ComparisonOperator.Equal, "Three")) _
	.AddWithOr(New FieldCompareValuePredicate(TableFields.Foo, ComparisonOperator.Equal, "Four"))
Fields and operators
Various operators are defined to work with the EntityField objects to form filters, or even expressions (See for expressions: Field expressions and aggregates). The list below will guide you what kind of object is created when you use the specified operator on an EntityField object.

C# VB.NET Descr. Example Object produced
+ + Addition (OrderDetailsFields.Quantity + 10) Expression
| Or SortClause construction (CustomerFields.CompanyName | SortOperator.Ascending) SortClause
/ / Division (OrderDetailsFields.Quantity / 10) Expression
== = Equality (CustomerFields.CompanyName == "Foo Inc.") FieldCompareValuePredicate, FieldCompareExpressionPredicate, FieldCompareRangePredicate, FieldCompareNullPredicate
> > Greater Than (OrderDetailsFields.Quantity > 10) FieldCompareValuePredicate, FieldCompareExpressionPredicate
>= >= Greater Than Or Equal (OrderDetailsFields.Quantity >= 10) FieldCompareValuePredicate, FieldCompareExpressionPredicate
!= <> Equality (CustomerFields.CompanyName != "Foo Inc.") FieldCompareValuePredicate, FieldCompareExpressionPredicate, FieldCompareRangePredicate, FieldCompareNullPredicate
< < Lesser Than (OrderDetailsFields.Quantity < 10) FieldCompareValuePredicate, FieldCompareExpressionPredicate
<= <= Lesser Than Or Equal (OrderDetailsFields.Quantity <= 10) FieldCompareValuePredicate, FieldCompareExpressionPredicate
% Mod Like creation (CustomerFields.CompanyName % "Foo%") FieldLikePredicate
* * Multiplication (OrderDetailsFields.Quantity * 10) Expression
- - Substraction (OrderDetailsFields.Quantity - 10) Expression

Not all predicate classes can be created with the operator overloads, for example FieldBetweenPredicate and FieldCompareSetPredicate aren't constructable using operator overloads like discussed in the previous table. Nevertheless, using the field construction classes and the Set*() methods, it will be easy to construct these predicates as well.

If you're using QuerySpec or add a using/Imports statement to your code file for including the QuerySpec namespace, you'll find additional extension methods for fields to produce predicates. See for the full list of predicate producers, the LLBLGen Pro runtime framework reference manual, QuerySpec namespace for the *PredicateProducers classes, as these contain the extension methods to use. As QuerySpec adds a lot of helper methods, it's not doable to describe all of them with the predicate classes below. Where possible some QuerySpec examples are given, using a .Where method call, which is a method call on a query object and which adds the predicate to the query as a Where clause.

Predicate classes for database queries or in-memory filtering

FieldBetweenPredicate

note Note:
The SQL examples given can contain absolute values. The SQL generated by the predicates will never contain absolute values as absolute values will be converted to parameters.

Description compares the entity field specified using a BETWEEN operator and results in true if the field's value is greater than or equal to the value of valueBegin and less than or equal to the value of valueEnd. valueBegin and valueEnd can be any value or an EntityField, as shown in the examples.
SQL equivalent Field BETWEEN valueStart AND valueEnd
Field BETWEEN OtherField AND valueEnd
Field BETWEEN valueStart AND OtherField
Operators none.
Example
  • C#
  • VB.NET
// C#
filter.Add(new FieldBetweenPredicate( OrderFields.OrderDate, dateStart, dateEnd));
// or: ShippingDate BETWEEN RequiredDate and dateEnd
filter.Add(new FieldBetweenPredicate( OrderFields.ShippingDate, OrderFields.RequiredDate, dateEnd));

// QuerySpec extension methods:
.Where(OrderFields.OrderDate.Between(dateStart, dateEnd));
// or: ShippedDate BETWEEN RequiredDate and dateEnd
.Where(OrderFields.ShippedDate.Between(OrderFields.requiredDate, dateEnd));
' VB.NET
filter.Add(new FieldBetweenPredicate( OrderFields.OrderDate, dateStart, dateEnd))
' or: ShippingDate BETWEEN RequiredDate and dateEnd
filter.Add(new FieldBetweenPredicate( OrderFields.ShippingDate, OrderFields.RequiredDate, dateEnd))

' QuerySpec extension methods:
.Where(OrderFields.OrderDate.Between(dateStart, dateEnd))
' or: ShippedDate BETWEEN RequiredDate and dateEnd
.Where(OrderFields.ShippedDate.Between(OrderFields.requiredDate, dateEnd))

Can be used for in-memory filtering Yes
FieldCompareExpressionPredicate
Description compares the entity field specified with the expression specified, using the ComparisonOperator specified. See for a detailed description about expressions the Field expressions and aggregates section
SQL equivalent examples Field > (OtherField * 2)
Field <= OtherField
Operators All ComparisonOperator operators: Equal, GreaterEqual, GreaterThan, LessEqual, LesserThan, NotEqual
Example This example creates a predicate which compares Order.OrderDate with Order.ShippingDate. The example might look a little verbose, but Expression objects are re-usable, which allows you to define the Expression objects once and re-use them each time you need them with a predicate class for example. The example illustrates a very basic expression, but the expression you can specify can be very complex. See for more information about expressions Field expressions and aggregates

  • C#
  • VB.NET
// C#
filter.Add(new FieldCompareExpressionPredicate(
	OrderFields.OrderDate, ComparisonOperator.Equal,
	new Expression(OrderFields.ShippedDate)));
	
// which is equal to:
filter.Add((OrderFields.OrderDate == OrderFields.ShippedDate));

// QuerySpec extension methods 
.Where(OrderFields.OrderDate.Equal(OrderFields.ShippedDate));
' VB.NET
filter.Add(New FieldCompareExpressionPredicate( _
	OrderFields.OrderDate, ComparisonOperator.Equal, _
	New Expression(OrderFields.ShippedDate)))
	
' which is equal to: (VB.NET)
filter.Add((OrderFields.OrderDate = OrderFields.ShippedDate))

' QuerySpec extension methods 
.Where(OrderFields.OrderDate.Equal(OrderFields.ShippedDate))

Example which filters on orders which have been shipped 4 days after the orderdate:

  • C#
  • VB.NET
// C#
filter.Add(new FieldCompareExpressionPredicate(
	OrderFields.ShippedDate, ComparisonOperator.Equal,
	new Expression(OrderFields.OrderDate, ExOp.Add, 4)));
	
// which is equal to:
filter.Add((OrderFields.ShippedDate == (OrderFields.OrderDate + 4)));

// QuerySpec extension methods
.Where(OrderFields.ShippedDate.Equal(OrderFields.OrderDate + 4));
' VB.NET
filter.Add(New FieldCompareExpressionPredicate( _
	OrderFields.ShippedDate, ComparisonOperator.Equal, _
	New Expression(OrderFields.OrderDate, ExOp.Add, 4)))
	
' which is equal to: (VB.NET)
filter.Add((OrderFields.ShippedDate == (OrderFields.OrderDate + 4)))

' QuerySpec extension methods
.Where(OrderFields.ShippedDate.Equal(OrderFields.OrderDate + 4))

Can be used for in-memory filtering Yes
FieldCompareNullPredicate
Description compares the entity field specified with NULL.
SQL equivalent Field IS NULL
Operators none.
Example
  • C#
  • VB.NET
// C#
filter.Add(new FieldCompareNullPredicate(OrderFields.OrderDate));
	
// which is equal to:
filter.Add((OrderFields.OrderDate==System.DBNull.Value));

// QuerySpec extension methods
.Where(OrderFields.OrderDate.IsNull());
' VB.NET
filter.Add(New FieldCompareNullPredicate(OrderFields.OrderDate))
	
' which is equal to: (VB.NET)
filter.Add((OrderFields.OrderDate = System.DBNull.Value))

' QuerySpec extension methods
.Where(OrderFields.OrderDate.IsNull())

Can be used for in-memory filtering Yes
FieldCompareRangePredicate
Description compares the entity field specified with the range of specified values using the IN operator. The range is not a subquery, use FieldCompareSetPredicate for that. The range can be supplied in an ArrayList, in an array or hardcoded in the predicate constructor.
SQL equivalent examples Field IN (1, 2, 5, 10)
Field IN ("Foo", "Bar", "Blah")
Operators none.
Example This example creates a predicate which compares Order.EmployeeId with the range 1, 2, 5, stored in an array. The values can also be specified directly in the constructor.

  • C#
  • VB.NET
// C#
int[] values = new int[3] {1, 2, 5};
filter.Add(new FieldCompareRangePredicate(OrderFields.EmployeeId, values));

// which is equal to:
filter.Add(OrderFields.EmployeeId == values);

// QuerySpec extension methods
.Where(OrderFields.EmployeeId.In(values));
' VB.NET
Dim values As Integer() = New Integer(2) {1, 2, 5}
filter.Add(New FieldCompareRangePredicate(OrderFields.EmployeeId, values))

' which is equal to (VB.NET)
filter.Add(OrderFields.EmployeeId = values)

' QuerySpec extension methods
.Where(OrderFields.EmployeeId.In(values))

Can be used for in-memory filtering Yes
FieldCompareSetPredicate
Description compares the entity field specified with the set of values defined by the subquery elements, using the SetOperator specified. The FieldCompareSetPredicate is the predicate you'd like to use when you want to compare a field's value with a range of values retrieved from another table / view (or the same table / view) using a subquery.

FieldCompareSetPredicates also allows you to define EXISTS () queries. It is then not necessary to specify an IEntityField object with the predicate's constructor (specify null / nothing) as it is ignored when building the SQL. Keep in mind that EXISTS() queries are semantically the same as IN queries and IN queries are often simpler to formulate.

The FieldCompareSetPredicate supports advanced comparison operators like ANY, ALL and combinations of these with comparison operators like Equal (=) or GreaterThan (>). If the set is just 1 value in size (because you've specified a limit on the number of rows to return), it's wise to use the Equal operator instead of the IN operator as most databases will be rather slow with IN and just 1 value compared to the Equal operator.
SQL equivalent examples Field IN (SELECT OtherField FROM OtherTable WHERE Foo=2)
EXISTS (SELECT * FROM OtherTable)
Operators All SetOperator operators: In, Exists, Equal, EqualAny, EqualAll, LessEqual, LessEqualAny, LessEqualAll, LesserThan, LesserThanAny, LesserThanAll, GreaterEqual, GreaterEqualAny, GreaterEqualAll, GreaterThan, GreaterThanAny, GreaterThanAll, NotEqual, NotEqualAny, NotEqualAll
Example This example illustrates the query: Customer.CustomerID IN (SELECT CustomerID FROM Orders WHERE Employee=2)

  • C#
  • VB.NET
// C#
filter.Add(new FieldCompareSetPredicate(
	CustomerFields.CustomerID, OrderFields.CustomerID,
	SetOperator.In, (OrderFields.EmployeeID == 2)));

// QuerySpec extension methods (qf is QueryFactory instance)
.Where(CustomerFields.CustomerID.In(
	qf.Create()
		.Select(OrderFields.CustomerId)
		.Where(OrderFields.EmployeeId==2));
' VB.NET
filter.Add(New FieldCompareSetPredicate( _
	CustomerFields.CustomerID, OrderFields.CustomerID, _
	SetOperator.In, (OrderFieldIndex.EmployeeID = 2)))

' QuerySpec extension methods (qf is QueryFactory instance)
.Where(CustomerFields.CustomerID.In( _
	qf.Create() _
		.Select(OrderFields.CustomerId) _
		.Where(OrderFields.EmployeeId=2))

Can be used for in-memory filtering No
FieldCompareValuePredicate
Description compares the entity field specified with the value specified, using the ComparisonOperator specified. If the value to compare is a string, you will get a case sensitive compare if the database is using a case sensitive collation (like Oracle). You can perform case insensitive compares however, by setting the CaseSensitiveCollation property to true prior to passing the predicate to a fetch method like GetMulti(). This will perform the UPPERCASE variant of the field with the pattern specified. Please note that if you've set CaseSensitiveCollaction to true, you've to specify your pattern in uppercase as well.
SQL equivalent examples Field > 3
Field = "Foo"
Operators All ComparisonOperator operators: Equal, GreaterEqual, GreaterThan, LessEqual, LesserThan, NotEqual
Example This example creates a predicate which compares Order.EmployeeID with the value 2

  • C#
  • VB.NET
// C#
filter.Add(new FieldCompareValuePredicate(OrderFields.EmployeeID, 
	ComparisonOperator.Equal, 2));
	
// which is equal to:
filter.Add(OrderFields.EmployeeID == 2);

// QuerySpec extension methods
.Where(OrderFields.EmployeeId.Equal(2));
' VB.NET
filter.Add(New FieldCompareValuePredicate(OrderFields.EmployeeID, _
	ComparisonOperator.Equal, 2))

' which is equal to:
filter.Add(OrderFields.EmployeeID = 2)

// QuerySpec extension methods
.Where(OrderFields.EmployeeId.Equal(2))

Can be used for in-memory filtering Yes.

note Note:
When using FieldCompareValuePredicate for in-memory filters, like with an EntityView, and you're using a value which is of a different type than the type of the field, the value to compare with is converted to the type of the field before the comparison. For example, if the field is of type Int64, and you specify as value to compare the value 1, you'll be comparing an Int64 with an Int32, which will result in the conversion of the Int32 value '1' to an Int64 value. If you don't want this conversion to happen, specify the value in the type of the field.

FieldFullTextSearchPredicate (SQL Server specific)
Description SqlServer specific. Compares the entity field specified with the pattern specified using the FullTextSearch operator specified.
SQL equivalent CONTAINS(Field, "Bla") FREETEXT(Field, "Bla")
Operators All FullTextSearchOperator operators: Contains, Freetext
Example The following example searches using SqlServer's Full Text search in the CustomerEntity's field CompanyName for the string "Solution"

  • C#
  • VB.NET
// C#
filter.Add(new FieldFullTextSearchPredicate(
	CustomerFields.CompanyName,	FullTextSearchOperator.Contains, "Solution"));
' VB.NET
filter.Add(new FieldFullTextSearchPredicate(
	CustomerFields.CompanyName,	FullTextSearchOperator.Contains, "Solution"))

The next example shows a filter on two fields, using the SQL Server 2005+ specific feature to accept multiple fields for the same operator.

  • C#
  • VB.NET
// C#
filter.Add(new FieldFullTextSearchPredicate(
	new IEntityField[] { CustomerFields.CompanyName, CustomerFields.ContactName},
	FullTextSearchOperator.Contains, "Solution"));
' VB.NET
filter.Add(New FieldFullTextSearchPredicate( _
	New IEntityField() { CustomerFields.CompanyName, CustomerFields.ContactName }, _
	FullTextSearchOperator.Contains, "Solution"))

Can be used for in-memory filtering No, use the FieldLikePredicate with a regular expression for full-text search capabilities.
FieldLikePredicate
Description compares the entity field specified with the pattern specified, using the LIKE operator. The pattern should contain the wildcard, which is '%' (also for MS Access). FieldLikePredicate performs a LIKE compare using the case sensitivity setting of the database system the query is executed on: the SQL generated does not contain any collation information nor any case insensitive setting if the database is using case sensitive comparison operations by default (Oracle, some SqlServer installations). You can perform case insensitive compares however, if the database is case sensitive, by setting the CaseSensitiveCollation property to true prior to passing the predicate to a fetch method like GetMulti(). This will perform the UPPERCASE variant of the field with the pattern specified. Please note that if you've set CaseSensitiveCollaction to true, you've to specify your pattern in uppercase as well.
SQL equivalent examples Field LIKE '%bla'
Field LIKE 'bla%'
Operators none.
Example This example creates a predicate which compares Customer.CompanyName to the pattern "Solution%".

  • C#
  • VB.NET
// C#
filter.Add(new FieldLikePredicate(CustomerFields.CompanyName, "Solution%"));

// Which is equal to:
filter.Add(CustomerFields.CompanyName % "Solution%");

// QuerySpec extension methods
.Where(CustomerFields.CompanyName.StartsWith("Solution"));
' VB.NET
filter.Add(New FieldLikePredicate(CustomerFields.CompanyName, "Solution%"))

' Which is equal to:
filter.Add(CustomerFields.CompanyName Mod "Solution%")

// QuerySpec extension methods
.Where(CustomerFields.CompanyName.StartsWith("Solution"))

Note, that the operator syntaxis is a little odd in VB.NET, due to the fact that there isn't an ability to add new operators to VB.NET/C#.
Can be used for in-memory filtering Yes. When used in in-memory filters, the pattern can either be a normal LIKE statement pattern with '%' wildcards, or it can be a full regular expression. If the pattern is a regular expression, be sure to set the property PatternIsRegEx to true. See also the LLBLGen Pro reference manual on more detailed information about the properties of the FieldLikePredicate

Predicate classes for in-memory filtering only

Below you'll find the predicate classes which are only usable for in-memory filtering.
AggregateSetPredicate
Description Predicate class which performs an aggregate function on a set of entities and returns true or false depending if that aggregated value
matches a specified expression. The set of entities this predicate is applied on, for example a collection of OrderEntity instances, are the elements of a member property with the name specified which match the specified filter, for example 'Orders' in a set of CustomerEntity instances.
Operators All ComparisonOperator operators: Equal, GreaterEqual, GreaterThan, LessEqual, LesserThan, NotEqual
Example This example filters a set of customers to find all customers with at least 10 orders. customers is a collection of CustomerEntity instances.
  • C#
  • VB.NET
// C#
IPredicate filter = new AggregateSetPredicate(	
	CustomerEntity.MemberNames.Orders,	// member to apply the aggregate on
	AggregateSetFunction.Count,	// aggregate function to apply. 
	OrderFields.OrderId,		// value producer. Can be a field as well.
	ComparisonOperator.GreaterThan,	// comparison operator for the aggregateset 
	10,				// value to compare with
	null);			// additional filter to apply to the set.
		
IEntityView filteredCustomers = new EntityView<CustomerEntity>(customers, filter);
' VB.NET
Dim filter As IPredicate = New AggregateSetPredicate( _ 
	CustomerEntity.MemberNames.Orders, _ 	' member to apply the aggregate on
	AggregateSetFunction.Count,	_ 	' aggregate function to apply. 
	OrderFields.OrderId, _			' value producer. Can be a field as well.
	ComparisonOperator.GreaterThan,	_	' comparison operator for the aggregateset
	10, _			' value to compare with
	Nothing)		' additional filter to apply to the set. 
		
Dim filteredCustomers As IEntityView = New EntityView(Of CustomerEntity)(customers, filter)
DelegatePredicate and DelegatePredicate<T>
Description Predicate class to filter in-memory entity collections based on a specified callback function. Use this predicate to filter entities based on logic which is best expressed in a normal .NET language, like C# or VB.NET.

The generic variant accepts a Predicate(Of T) (.NET 2.0+) or Lambda expression (.NET 3.5+) which is then used as a filter.
Operators None
Example This example filters a set of customers which have a CompanyName with length of 20 or higher. customers is a collection of CustomerEntity instances.
  • C#
  • VB.NET
// C#
// Create an entity view from customers based on the DelegatePredicate filter. 
// Uses an anonymous method, which is a .NET 2.0+ feature.
IEntityView customersWithLongName = new EntityView<CustomerEntity>(customers,
	new DelegatePredicate(
	  delegate(IEntityCore toExamine) 
	  { 
		  return ((CustomerEntity)toExamine).CompanyName.Length > 20; 
	  }));
' VB.NET
' VB.NET doesn't support anonymous methods, so we need a separate method. 
Public Function CompareCustomerCompanyName(ByVal toExamine As CustomerEntity) As Boolean
	Return toExamine.CompanyName.Length > 20
End Function
' .. somewhere in your code where you want to filter on the CompanyName length Dim customersWithLongName As IEntityView = New EntityView(Of CustomerEntity)(customers, _ new DelegatePredicate(AddressOf CompareCustomerCompanyName))

For a .NET 3.5+ example, please see the EntityView filtering example.
MemberPredicate
Description Predicate class which allows in-memory filters to perform a predicate (filter) on one or more related entities. The entity this predicate
is applied on has to have a member property with the name specified. Each element in that member (or the member itself, in case of a
single instance) will be interpreted with the specified filter. The result of that interpretation is used together with the MemberOperator specified what the result of this predicate will be: true or false, in which case the entity this predicate is applied on is accepted (true) or not (false).
Operators All MemberOperator operators: All, Any or Null (no data)
Example This example shows in memory filtering of customers which filters all customers which have orders with a total > 5000. The order total is calculated by SUMming the orderdetail total values. The order detail total is (UnitPrice * quantity) - ((UnitPrice * quantity)* discount). customers is a collection of CustomerEntity instances.

  • C#
  • VB.NET
// C#
IPredicate filter = new MemberPredicate(
    CustomerEntity.MemberNames.Orders,    // the member to apply the specified filter on.
    MemberOperator.Any,                   // operator for applying contained filter.
    new AggregateSetPredicate(            // aggregate predicate to apply on a member
        OrderEntity.MemberNames.OrderDetails,        // member to apply the aggregate on
        AggregateSetFunction.Sum,        // aggregate function to apply. 
        ((OrderDetailsFields.UnitPrice * OrderDetailsFields.Quantity)-
            ((OrderDetailsFields.UnitPrice * OrderDetailsFields.Quantity) 
                * OrderDetailsFields.Discount)),        // value producer expression. 
        ComparisonOperator.GreaterThan,    // comparison operator for the aggregateset 
        5000.0M,        // value to compare with
        null            // filter which specifies the set to apply the aggregate on.
        )                    
    );

IEntityView filteredCustomers = new EntityView<CustomerEntity>(customers, filter);
' VB.NET
Dim filter As IPredicate = New MemberPredicate( _
    CustomerEntity.MemberNames.Orders,    _ ' the member to apply the specified filter on.
    MemberOperator.Any, _                  ' operator for applying contained filter.
    New AggregateSetPredicate( _        ' aggregate predicate to apply on a member
        OrderEntity.MemberNames.OrderDetails, _        ' member to apply the aggregate on
        AggregateSetFunction.Sum, _        ' aggregate function to apply. 
        ((OrderDetailsFields.UnitPrice * OrderDetailsFields.Quantity)- _
            ((OrderDetailsFields.UnitPrice * OrderDetailsFields.Quantity)  _
                * OrderDetailsFields.Discount)), _        ' value producer expression. 
        ComparisonOperator.GreaterThan,    _    ' comparison operator for the aggregateset 
        5000.0D, _            ' value to compare with
        Nothing    _            ' filter which specifies the set to apply the aggregate on.
        ))

Dim filteredCustomers As new EntityView(Of CustomerEntity)(customers, filter)

LLBLGen Pro Runtime Framework v4.2 documentation. ©2002-2014 Solutions Design bv